﻿$.fn.FileUpload = {
    Init: function (obj) {
        // 初始化Web Uploader
        var uploader = WebUploader.create({
            duplicate: true, //同一个文件可以多次上传
            auto: true,// 自动上传。
            swf: '/Js/plugin/webuploader/Uploader.swf',// swf文件路径
            server: '/Common/Upload.ashx?mode=file',// 文件接收服务端。
            pick: {
                id: '#' + obj.uploaderID,
                multiple: false
            },
            fileSingleSizeLimit: obj.FileSize * 1024 * 1024,   //设定单个文件大小
            //chunked: true,
            //threads: 1,
            //formData: { guid: moment().format("YYYYMMDDHHmmssSSS") },

            // 只允许选择文件，可选。
            accept: {
                title: '文件',
                extensions: obj.extensions,
                mimeTypes: '*/*'
            }
        });
        uploader.on('fileQueued', function (file) {
            if ($.fn.FileUpload.GetRows(obj).length >= obj.MaxCount) {
                uploader.removeFile(file, true);
                $.messager.alert('提示信息', '只能上传一个文件!');
            }
        });
        uploader.on('uploadStart', function () {
            $('#' + obj.progressbarID).progressbar({
                width: 400,
                height: 15,
                value: 0
            });
        });
        uploader.on('uploadProgress', function (file, percentage) {
            $('#' + obj.progressbarID).progressbar('setValue', Math.ceil(percentage * 100));
        });

        //文件上传成功
        uploader.on('uploadSuccess', function (file, ret) {
            var url = "/Themes/images/icon/default.png";
            if (ret.type == ".zip" || ret.type == ".rar") {
                url = "/Themes/images/icon/default.png";
            }
            else if (ret.type == ".txt") {
                url = "/Themes/images/icon/txt.png";
            }
            else if (ret.type == ".pdf") {
                url = "/Themes/images/icon/pdf.png";
            }
            else if (ret.type == ".doc" || ret.type == ".docx") {
                url = "/Themes/images/icon/doc.png";
            }
            else if (ret.type == ".ppt" || ret.type == ".pptx") {
                url = "/Themes/images/icon/ppt.png";
            }
            else if (ret.type == ".xls" || ret.type == ".xlsx") {
                url = "/Themes/images/icon/xls.png";
            }
            else if (ret.type == ".jpg" || ret.type == ".jpeg" || ret.type == ".png") {
                url = ret.url;
            }

            $('#' + obj.id).datagrid('appendRow', {
                FileImg: "<img src='" + url + "' width='56px' height='56px'>",
                FileName: ret.name,
                FileType: ret.type,
                FileUrl: ret.url
            });
        });

        //uploader.on('uploadComplete', function (file) {
        //    alert("完成");
        //});
        //uploader.on('uploadError', function (file) {
        //    alert("失败");
        //});

        uploader.on("error", function (type) {
            if (type == "Q_TYPE_DENIED") {
                alert("上传文件格式不正确,可以上传'" + obj.extensions + "'");
            } else if (type == "F_EXCEED_SIZE") {
                alert("文件大小不能超过" + obj.FileSize + "M");
            }
        });

    },
    DataBind: function (obj) {
        //必须属性
        obj.id = obj.id;
        obj.uploaderID = moment().format("YYYYMMDDHHmmssSSS");
        obj.progressbarID = 'jdt' + moment().format("YYYYMMDDHHmmssSSS");

        obj.key = $.SetAttValue(obj, "key", "ID");
        obj.tableName = $.SetAttValue(obj, "tableName", "Info");
        obj.Data = $.SetAttValue(obj, "Data", []);
        obj.FileSize = $.SetAttValue(obj, "FileSize", 10);//文件大小
        if (typeof (obj.extensions) != "undefined")
        {
            obj.extensions = $.SetAttValue(obj, "extensions", obj.extensions);//"jpg,jpeg,bmp,png,zip,rar,pdf,txt,xls,xlsx,doc,docx,ppt,gif"
        } else {
            obj.extensions = $.SetAttValue(obj, "extensions", "jpg,jpeg,bmp,png,zip,rar,pdf,txt,xls,xlsx,doc,docx,ppt,gif");
        }
        obj.MaxCount = $.SetAttValue(obj, "MaxCount", 20);//最大文件数量

        $('#' + obj.id).datagrid({
            fit: true,
            singleSelect: true,
            fitColumns: true,
            data: obj.Data,
            columns: [[
                { field: 'FileImg', title: '图标', width: 100 },
                { field: 'FileName', title: '名称', width: 150 },
                { field: 'FileType', title: '类型', width: 100 },
                { field: 'FileUrl', title: '地址', width: 350 }
            ]],
            toolbar: [{
                id: obj.uploaderID,
                text: '浏览文件...'
            }, {
                iconCls: 'icon-table_delete',
                text: '删除',
                handler: function () {
                    var row = $('#' + obj.id).datagrid('getSelected');
                    if (row != null) {
                        $.messager.confirm('提示信息', '您确定要删除选定的记录吗？', function (r) {
                            if (r) {
                                var index = $('#' + obj.id).datagrid('getRowIndex', row);
                                $('#' + obj.id).datagrid('deleteRow', index);
                            }
                        });
                    }
                }
            }
            , {
                text: '<div id="' + obj.progressbarID + '"></div>'
            }
            ]
        });


        $.fn.FileUpload.Init(obj);
    },
    GetRows: function (obj) {
        var rows = $('#' + obj.id).datagrid('getRows');
        return rows;
    }
}; 